package com.shopping.controller;

import com.shopping.common.Result;
import com.shopping.entity.CartItem;
import com.shopping.service.CartService;
import com.shopping.util.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Resource
    private CartService cartService;

    private Long getCurrentUserId() {
        return SecurityUtils.getCurrentUserId();
    }

    @GetMapping("/list")
    public Result<List<CartItem>> getCartList() {
        try {
            Long userId = getCurrentUserId();
            if (userId == null) {
                return Result.error("用户未登录");
            }

            List<CartItem> cartItems = cartService.getUserCart(userId);
            return Result.success(cartItems);
        } catch (Exception e) {
            return Result.error("获取购物车失败: " + e.getMessage());
        }
    }

    @PostMapping("/add")
    public Result<Boolean> addToCart(@RequestBody Map<String, Object> cartItem) {
        Long userId = getCurrentUserId();
        Long productId = Long.valueOf(cartItem.get("productId").toString());
        Integer quantity = Integer.valueOf(cartItem.get("quantity").toString());

        boolean success = cartService.addToCart(userId, productId, quantity);
        return success ? Result.success(true) : Result.error("添加购物车失败");
    }

    @PutMapping("/{id}")
    public Result<Boolean> updateCartItem(@PathVariable Long id, @RequestBody Map<String, Object> data) {
        Long userId = getCurrentUserId();
        Integer quantity = Integer.valueOf(data.get("quantity").toString());
        boolean success = cartService.updateCartItem(userId, id, quantity);
        return success ? Result.success(true) : Result.error("更新购物车失败");
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> removeCartItem(@PathVariable Long id) {
        Long userId = getCurrentUserId();
        boolean success = cartService.removeCartItem(userId, id);
        return success ? Result.success(true) : Result.error("删除购物车商品失败");
    }

    @DeleteMapping("/clear")
    public Result<Boolean> clearCart() {
        Long userId = getCurrentUserId();
        boolean success = cartService.clearCart(userId);
        return success ? Result.success(true) : Result.error("清空购物车失败");
    }

    @GetMapping("/count")
    public Result<Integer> getCartCount() {
        Long userId = getCurrentUserId();
        Integer count = cartService.getCartCount(userId);
        return Result.success(count);
    }

    @DeleteMapping("/batch-remove")
    public Result<Boolean> batchRemoveCartItems(@RequestBody Map<String, Object> data) {
        Long userId = getCurrentUserId();
        @SuppressWarnings("unchecked")
        List<Long> ids = (List<Long>) data.get("ids");
        boolean success = cartService.batchRemoveCartItems(userId, ids);
        return success ? Result.success(true) : Result.error("批量删除失败");
    }

    @PostMapping("/checkout")
    public Result<Boolean> checkoutCart(@RequestBody Map<String, Object> data) {
        Long userId = getCurrentUserId();
        boolean success = cartService.checkoutCart(userId, data);
        return success ? Result.success(true) : Result.error("购物车结算失败");
    }

    @GetMapping("/statistics")
    public Result<Map<String, Object>> getCartStatistics() {
        Long userId = getCurrentUserId();
        Map<String, Object> statistics = cartService.getCartStatistics(userId);
        return Result.success(statistics);
    }
}